﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TSV.Library.Common;
using TSV.Areas.Administration.Models;
using TSV.Models;
using TSV.Library.Attributes;

namespace TSV.Areas.Administration.Controllers
{
    public class BusinessRegisterController : BaseController
    {
        //
        // GET: /Administration/BusinessRegister/
        [CrmAuthorizeAttribute(Module = Modules.QL_DK_Lam_Doi_Tac, Rights = Permissions.Doc)]
        public ActionResult Index()
        {
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem{Text = "Tất cả",Value = ""});
            items.Add(new SelectListItem{Text = "Chưa liên hệ",Value = "0"});
            items.Add(new SelectListItem{Text = "Đã liên hệ",Value = "1"});
            ViewData["ContactList"] = items;
            return View();
        }
        BusinessRegisterDao BR = new BusinessRegisterDao();
        public ActionResult GetListJQGrid(string keyword, string from_date, string to_date, string isContacted)
        {

            #region JQGrid Params

            string sortColumn = (Request.Params[GridConstants.SORT_COLUMN]).ToString();
            string sortOrder = (Request.Params[GridConstants.SORT_ORDER]).ToString();
            int pageIndex = Convert.ToInt32(Request.Params[GridConstants.PAGE_INDEX]);
            int rowCount = Convert.ToInt32(Request.Params[GridConstants.ROW_COUNT]);
            #endregion
            #region search
            string key = String.Empty;
            string from = String.Empty;
            string to = String.Empty;
            string contacted = String.Empty;
            if (!string.IsNullOrEmpty(keyword))
            {
                key = keyword;
            }
            if (!string.IsNullOrEmpty(from_date))
            {
                from = from_date;
            }

            if (!string.IsNullOrEmpty(to_date))
            {
                to = to_date;
            }
            if (!string.IsNullOrEmpty(isContacted))
            {
                contacted = isContacted;
            }
            #endregion
            List<sp_GetBusinessRegisterResult> BRList = BR.GetList(key, from, to, contacted).ToList<sp_GetBusinessRegisterResult>();

            int totalRecords = BRList.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)rowCount);

            var finalList = BR.Sort(BRList, sortColumn, sortOrder)
                                  .Skip((pageIndex - 1) * rowCount)
                                   .Take(rowCount);
            int j = 0;

            var jsonData = new
            {
                total = totalPages,
                page = pageIndex,
                records = totalRecords,
                rows = (
                    from m in finalList
                    select new
                    {

                        i = m.ID,
                        cell = new string[] {
                                Convert.ToString(j+=1),
                                m.ID.ToString(), 
                                TooltipLink(m.ID.ToString(),"/Administration/BusinessRegister/Edit",HttpUtility.HtmlEncode(m.BusinessName),"Cập nhật","500"),
                                HttpUtility.HtmlEncode(m.Representative),
                                HttpUtility.HtmlEncode(m.Service),
                                HttpUtility.HtmlEncode(m.Email),
                                HttpUtility.HtmlEncode(m.Phone),
                                HttpUtility.HtmlEncode(m.Address),
                                HttpUtility.HtmlEncode(m.Website),
                                HttpUtility.HtmlEncode(m.CreateDate.ToString(Constants.DATETIME_FORMAT)),
                                CommonFunc.ShowActiveImage(m.IsContacted),
                                CommonFunc.ButtonEditPopup("/Administration/BusinessRegister/Edit/"+m.ID.ToString(),"Cập nhật",500),
                                
                            }
                    }
                ).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
        private string TooltipLink(string id, string actionUrl, string textLink, string tilte, string width)
        {
            string onclick = string.Format("JS_LIB.popup('{0}/{1}','{2}',{3});", actionUrl, id, tilte, width);
            string jsvoid = "javascript:void(0);";
            return string.Format("<a id=\"{0}\" href=\"{1}\" class=\"showTooltip\" onclick=\"{2}\">{3}</a>", id, jsvoid, onclick, textLink);

        }
        private string ViewButtonEdit(string actionUrl)
        {
            string onclick = string.Format("JS_LIB.popup('{0}','{1}',{2});", actionUrl, "Cập nhật", 500);

            return CommonFunc.ButtonEditPopup(actionUrl, "Cập nhật", 500);

        }
        [CrmAuthorizeAttribute(Module = Modules.QL_DK_Lam_Doi_Tac, Rights = Permissions.Xoa, ShowAtCurrentPage = true)]
        public ActionResult DeleteList(string id)
        {
            Message msg = BR.DeleteList(id);
            ShowMessage(msg);
            return RedirectToAction("Index");
        }
        [CrmAuthorizeAttribute(Module = Modules.QL_DK_Lam_Doi_Tac, Rights = Permissions.Doc)]
        public ActionResult Edit(int id)
        {
            BusinessRegister oscObj = BR.GetById(id.ToString());
            return View(oscObj);
        }
        [CrmAuthorizeAttribute(Module = Modules.QL_DK_Lam_Doi_Tac, Rights = Permissions.Sua, ShowAtCurrentPage=true)]
        [HttpPost]
        public ActionResult Edit(BusinessRegister oscObj, FormCollection collection)
        {
            try
            {
                
                // TODO: Add update logic here
                Message msg = BR.Update(oscObj);
                ShowMessage(msg);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
